package com.novunda.platform.services;

import com.novunda.platform.common.handler.LogCallBack;
import com.novunda.platform.common.utils.UserUtils;
import com.novunda.platform.common.web.modal.Page;
import com.novunda.platform.entities.Log;
import com.novunda.platform.entities.Role;
import com.novunda.platform.entities.User;
import com.novunda.platform.entities.base.BaseEntity;
import com.novunda.platform.repositories.RoleRepository;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * .
 * 功能说明：角色service
 * @author linfeng
 * @see 2016年9月22日
 * @version 1.0
 * @modify FJ
 */
@Service
@Transactional
public class RoleService extends BaseService {

	/**
	 * .
	 * 角色dao
	 */
	@Autowired
	private RoleRepository roleRepository;


	/**
	 * .
	 *功能说明：查询所有角色
	 * @return 角色集合
	 */
	public List<Role> findAllRole() {
		User user = UserUtils.getUser();
		DetachedCriteria dc = roleRepository.createDetachedCriteria();
		dc.createAlias("office", "office");
		dc.createAlias("userList", "userList", JoinType.LEFT_OUTER_JOIN);
		dc.add(dataScopeFilter(user, "office", "userList"));
		dc.add(Restrictions.eq(BaseEntity.DEL_FLAG, BaseEntity.DEL_FLAG_NORMAL));
		dc.addOrder(Order.asc("office.code")).addOrder(Order.asc("name"));
		return roleRepository.find(dc);
	}

	/**
	 * .
	 *功能说明：根据id查询
	 * @param id 角色id
	 * @return 角色对象
	 */
	public Role getRole(Long id) {
		return roleRepository.get(id);
	}

	/**
	 * .
	 *功能说明：通过角色名查询
	 * @param name 角色名
	 * @return
	 */
	public Role findRoleByName(String name) {
		return roleRepository.findByName(name);
	}

	/**
	 * .
	 *功能说明：删除角色
	 * @param id 角色id
	 */
	@Transactional(readOnly = false)
	public void deleteRole(final Long id) {
		handler.doHandler(new LogCallBack() {
            @Override
            public Object doHandler() {
            	roleRepository.deleteById(id);
                return null;
            }

            @Override
            public Log generate() {
                Log log = new Log();
                log.setType("删除");
                log.setName("删除角色");
                log.setDescription("删除角色id:" + id);
                return log;
            }

            @Override
            public boolean forceLog() {
                return false;
            }
        });
	}

	/**
	 * .
	 *功能说明：角色分页查询
	 * @param page 分页对象
	 * @param role 角色对象
	 * @return
	 */
	public Page<Role> findRolePage(Page<Role> page, Role role) {
		roleRepository.clear();
        User user = UserUtils.getUser();
        DetachedCriteria dc = roleRepository.createDetachedCriteria();
        dc.createAlias("office", "office");
        String keyword = role.getName();
        if (StringUtils.isNotEmpty(keyword)) {
        	keyword = keyword.trim();
        	dc.add(Restrictions.or(Restrictions.like("name", "%" + keyword + "%"),
        			Restrictions.like("office.name", "%" + keyword + "%")));
        }
        dc.add(dataScopeFilter(user, "office", ""));
        dc.add(Restrictions.eq(BaseEntity.DEL_FLAG, BaseEntity.DEL_FLAG_NORMAL));
        dc.addOrder(Order.desc("createDate"));
		return roleRepository.find(page, dc);
	}
	
	/**
	 * .
	 *功能说明：根据机构id查询该机构下
	 * @param orgId
	 * @return
	 */
	public List<Role> findRoleByOrg(long orgId) {
		roleRepository.clear();
		return roleRepository.findByOffice(orgId);
	}
	
}
